#!/bin/bash
clear

RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
BLUE="\033[1;34m"
PLAIN="\033[0m"

if [ "${EUID}" -ne 0 ]; then
    echo -e "${RED}You need to run this script as root!${PLAIN}" && exit 1
fi

IPADDR=$(wget -qO- icanhazip.com)
DOMAIN=$(cat /etc/environment | grep 'DOMAIN' | cut -d '=' -f 2 | head -n 1)
read -p "Enter username: " USERNAME
egrep "^$USERNAME" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
    echo -e "${RED}Username already exist!${PLAIN}" && exit 0
fi
read -p "Enter password: " PASSWORD
read -p "Active duraion [Day]: " ACTIVETIME
EXPDATE=$(date -d "$ACTIVETIME days" +"%F")

useradd $USERNAME
usermod -s /bin/false $USERNAME
usermod -e $EXPDATE $USERNAME
echo -e "$PASSWORD\n$PASSWORD" | passwd $USERNAME &>/dev/null

clear
echo
echo "===================== [ ADD: ACCOUNT DETAIL ]"
echo -e "${YELLOW}  Address:${PLAIN} ${GREEN}$IPADDR${PLAIN}"
echo -e "${YELLOW}   Domain:${PLAIN} ${GREEN}$DOMAIN${PLAIN}"
echo -e "${YELLOW} Username:${PLAIN} ${GREEN}$USERNAME${PLAIN}"
echo -e "${YELLOW} Password:${PLAIN} ${GREEN}$PASSWORD${PLAIN}"
echo -e "${YELLOW} Duration:${PLAIN} ${GREEN}$ACTIVETIME days${PLAIN}"
echo -e "${YELLOW}  Expired:${PLAIN} ${GREEN}$EXPDATE${PLAIN}"
echo "---------------------------------------------"
echo "Copyright 2021 Cybertize, Allright Reserved."
echo "============================================="
echo
